package com.free.fs.controller;

import com.free.fs.common.domain.R;
import com.free.fs.model.FilePojo;
import com.free.fs.service.PublicFileService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 公共文件访问控制器（无需登录）
 *
 * @author dinghao
 * @date 2024/01/01
 */
@RestController
@RequestMapping("public")
@RequiredArgsConstructor
public class PublicFileController {

    private final PublicFileService publicFileService;

    /**
     * 获取公共文件列表
     *
     * @param path 目录路径
     * @return
     */
    @GetMapping("/files")
    public R<List<FilePojo>> getFileList(@RequestParam(required = false, defaultValue = "") String path) {
        List<FilePojo> files = publicFileService.getFileList(path);
        return R.succeed(files, "查询成功");
    }

    /**
     * 下载文件
     *
     * @param fileId 文件ID
     * @param response
     */
    @GetMapping("/download/{fileId}")
    public void downloadFile(@PathVariable Long fileId, 
                           @RequestParam(required = false) String fileName, 
                           HttpServletResponse response) {
        publicFileService.downloadFile(fileId, fileName, response);
    }

    /**
     * 根据文件路径下载
     *
     * @param filePath 文件路径
     * @param fileName 自定义文件名（可选）
     * @param response
     */
    @GetMapping("/downloadByPath")
    public void downloadByPath(@RequestParam String filePath, 
                              @RequestParam(required = false) String fileName, 
                              HttpServletResponse response) {
        publicFileService.downloadByPath(filePath, fileName, response);
    }

    /**
     * 获取软件分类列表
     *
     * @return
     */
    @GetMapping("/categories")
    public R<List<FilePojo>> getSoftwareCategories() {
        List<FilePojo> categories = publicFileService.getSoftwareCategories();
        return R.succeed(categories, "获取成功");
    }
}
